package org.lim.lcpicturebackend.manager.websocket.interceptor;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.lim.lcpicturebackend.manager.auth.SpaceUserAuthManager;
import org.lim.lcpicturebackend.manager.auth.model.SpaceUserPermissionConstant;
import org.lim.lcpicturebackend.model.entity.Picture;
import org.lim.lcpicturebackend.model.entity.Space;
import org.lim.lcpicturebackend.model.entity.User;
import org.lim.lcpicturebackend.model.enums.SpaceTypeEnum;
import org.lim.lcpicturebackend.service.PictureService;
import org.lim.lcpicturebackend.service.SpaceService;
import org.lim.lcpicturebackend.service.UserService;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class WsHandshakeInterceptor implements HandshakeInterceptor {
    @Resource
    private UserService userService;
    @Resource
    private SpaceService  spaceService;
    @Resource
    private PictureService pictureService;
    @Resource
    private SpaceUserAuthManager  spaceUserAuthManager;
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        if(request instanceof ServletServerHttpRequest){
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            //获取请求参数
            String pictureId = servletRequest.getParameter("pictureId");
            if(StrUtil.isBlank(pictureId)){
                log.error("缺少图片参数，拒绝握手");
                return false;
            }
            User loginUser = userService.getLoginUser(servletRequest);
            if(ObjUtil.isEmpty(loginUser)){
                log.error("用户未登录，拒绝握手");
                return false;
            }
            //校验用户是否有编辑权限
            Picture picture = pictureService.getById(pictureId);
            if(ObjUtil.isNull(picture)){
                log.error("图片不存在，拒绝握手");
                return false;
            }
            Long spaceId = picture.getSpaceId();
            Space space = null;
            if(spaceId != null){
                space = spaceService.getById(spaceId);
                if(ObjUtil.isNull(space)){
                    log.error("空间不存在，拒绝握手");
                    return false;
                }
                if(space.getSpaceType() != SpaceTypeEnum.TEAM.getValue()){
                    log.error("非团队空间，拒绝握手");
                    return false;
                }
            }
            List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
          if(!permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)){
              log.error("用户无编辑图片权限，拒绝握手");
              return false;
          }
          attributes.put("user", loginUser);
          attributes.put("userId",  loginUser.getId());
          attributes.put("pictureId",  Long.valueOf(pictureId));

        }
        return true;


    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
